package cn.com.mjsoft.framework.persistence.core.type;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;

/**
 * sql Timestamp类型处理
 * 
 * @author mjsoft
 * 
 */
public class SqlTypeTimestamp extends AbstractCustomTypeValue
{

    public SqlTypeTimestamp( Integer sqlType, String typeName, Object value )
    {
        super( sqlType, typeName, value );
    }

    public void setTypeValue( PreparedStatement ps, int paramIndex )
        throws SQLException
    {
        if( value instanceof java.util.Date )
        {
            if( value instanceof java.sql.Timestamp )
            {
                ps.setTimestamp( paramIndex, ( java.sql.Timestamp ) value );
            }
            else
            {
                ps.setTimestamp( paramIndex, new java.sql.Timestamp(
                    ( ( java.util.Date ) value ).getTime() ) );
            }
        }
        else if( value instanceof Calendar )
        {
            Calendar cal = ( Calendar ) value;
            ps.setTimestamp( paramIndex, new java.sql.Timestamp( cal.getTime()
                .getTime() ), cal );
        }
        else
        {
            ps.setObject( paramIndex, value, Types.TIMESTAMP );
        }

    }
}
